package com.lxb.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.lxb.commonutils.R;
import com.lxb.servicebase.exceptionhandler.GuliException;
import com.lxb.vod.service.VodService;
import com.lxb.vod.util.ConstantPropertiesUtils;
import com.lxb.vod.util.InitAcsClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 阿里云视频点播控制层
 *
 * @author xuebin.li
 * @createTime 2022/11/29 21:39
 */

@Api(description = "阿里云视频点播控制层")
@RestController
@RequestMapping("/eduVod/video")
//@CrossOrigin
public class VodController {

    @Autowired
    private VodService vodService;


    @ApiOperation(value = "文件流上传视频")
    @PostMapping("/upLoadVideo")
    public R upLoadVideo(MultipartFile file) {

        String videoId = null;
        //本地上传视频，返回上传视频id编号
        videoId = vodService.upLoadVideoAliYun(file);
        return R.ok().data("videoId", videoId);
    }

    @ApiOperation(value = "根据视频id，删除阿里云云端视频")
    @DeleteMapping("/removeVideo/{videoId}")
    public R deleteVideoByID(@PathVariable String videoId) {

        try {
            //初始化 client 对象
            DefaultAcsClient client = InitAcsClient.initVodClient(ConstantPropertiesUtils.ACCESS_KEY_ID, ConstantPropertiesUtils.ACCESS_KEY_SECRET);
            //创建删除视频 request 和 response 对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            DeleteVideoResponse response = new DeleteVideoResponse();

            //向request对象设置视频id
            request.setVideoIds(videoId);
            //client 执行删除
            response = client.getAcsResponse(request);

            System.out.print("RequestId = " + response.getRequestId() + "\n");
            return R.ok();
        } catch (ClientException e) {
            throw new GuliException(20001, "删除视频失败");
        }
    }

    @ApiOperation("删除多个视频")
    @DeleteMapping("/deleteBath")
    public R deleteBath(@RequestParam("vodIds")List<String> vodIds){

        vodService.deleteBath(vodIds);
       return R.ok();
    }

    @ApiOperation("根据视频id,获取视频播放凭证")
    @GetMapping("/getVideoPlayAuth/{id}")
    public R getVideoPlayAuth(@PathVariable String id){

        //初始化 client 对象
        try {
            DefaultAcsClient client = InitAcsClient.initVodClient(ConstantPropertiesUtils.ACCESS_KEY_ID, ConstantPropertiesUtils.ACCESS_KEY_SECRET);

            //创建获取视频凭证的 request 对象
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest ();
            //向request设置请求的视频id
            request.setVideoId(id);

            //获取视频凭证
            GetVideoPlayAuthResponse response = client.getAcsResponse(request);
            String playAuth = response.getPlayAuth();

            return R.ok().data("playAuth",playAuth);
        } catch (ClientException e) {
           throw new GuliException(20001,"获取凭证失败");
        }
    }
}
